package com.antdesignbackend.backend.controller;

import com.antdesignbackend.backend.aop.AuthToken;
import com.antdesignbackend.backend.common.returnResult.ReturnVO;
import com.antdesignbackend.backend.entity.CertificationInformation;
import com.antdesignbackend.backend.entity.entityVo.CertificationInformationVo;
import com.antdesignbackend.backend.service.CertificationInformationService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/Certification")
public class CertificationController {
    private Logger log = LoggerFactory.getLogger(this.getClass());
    @Autowired
    CertificationInformationService certificationInformationService;
    ReturnVO result=new ReturnVO();
    /**
     *认证信息
     */
    @GetMapping("/index")
    @AuthToken
    public String index() {
        return "certification/index";
    }
    /**
     * 认证信息提交
     */
    @PostMapping("/setCertificationInformation")
    @ResponseBody
    public ReturnVO setCertificationInformation(CertificationInformation certificationInformation, HttpServletRequest httpServletRequest){
        if(certificationInformation.getOpenId()!=null&&!certificationInformation.getOpenId().equals("")){
            if(httpServletRequest.getSession().getAttribute("openid")!=null){
                //httpServletRequest.getSession().getAttribute("openid").toString();
                certificationInformation.setOpenId(httpServletRequest.getSession().getAttribute("openid").toString());
            }
        }
        boolean flag=false;
        ReturnVO returnVO=new ReturnVO();
        try{
            flag=certificationInformationService.save(certificationInformation);
            if(flag){
                returnVO.setMessage("提交成功！");
                returnVO.setCode("200");
            }else{
                returnVO.setMessage("提交失败！");
                returnVO.setCode("500");
            }
        }catch(Exception e){
            log.error(e.getMessage());
        };
        return returnVO;
    }
    /**
     * 表格查询
     */
    @PostMapping("/tableSearch")
    @ResponseBody
    public ReturnVO tableSearch(@RequestBody CertificationInformation certificationInformation) throws NoSuchMethodException, SecurityException,
            IllegalAccessException, IllegalArgumentException, InvocationTargetException {
        System.out.println("*******************************进入认证信息的表格查询");
        List<CertificationInformationVo> certificationInformationVos=new ArrayList<>();
        QueryWrapper<CertificationInformation> wrapper =new QueryWrapper<>();
        if(certificationInformation.getName()!=null&& !certificationInformation.getName().equals(""))
        {
            wrapper.like("a.name",certificationInformation.getName());
        }
        if(certificationInformation.getCity()!=null&& !certificationInformation.getCity().equals(""))
        {
            wrapper.like("a.city",certificationInformation.getCity());
        }
        //先统计满足条件的数据总数，因为sql 不包括排序
        int total=certificationInformationService.count(wrapper);
        result.setTotal(total);
        System.out.println("*******************************满足条件的数据有："+total);
        //再查询
        if(total>=0)
        {
            System.out.println("******************************进入if语句");
            certificationInformationVos=certificationInformationService.search(wrapper);
            System.out.println("*******************************满足条件的认证信息有："+certificationInformationVos);
        }
        result.setData(certificationInformationVos);
        return result;
    }
    /**
     * 认证
     */
    @GetMapping("/attestationTrue/{shopId}")
    @ResponseBody
    public ReturnVO attestationTrue(@PathVariable("shopId") int shopId){
        boolean flag=false;
        ReturnVO returnVO=new ReturnVO();
        try{
            UpdateWrapper<CertificationInformation> updateWrapper=new UpdateWrapper();
            updateWrapper.set("activity",1).eq("id",shopId);
            flag=certificationInformationService.attestationTrue(updateWrapper);
            if(flag){
                returnVO.setMessage("认证成功！");
                returnVO.setCode("200");
            }else{
                returnVO.setMessage("认证失败！");
                returnVO.setCode("500");
            }
        }catch(Exception e){
            log.error(e.getMessage());
        };
        return returnVO;
    }
    /**
     * 取消认证
     */
    @GetMapping("/attestationFalse/{shopId}")
    @ResponseBody
    public ReturnVO attestationFalse(@PathVariable("shopId") int shopId){
        boolean flag=false;
        ReturnVO returnVO=new ReturnVO();
        try{
            UpdateWrapper<CertificationInformation> updateWrapper=new UpdateWrapper();
            updateWrapper.set("activity",0).eq("id",shopId);
            flag=certificationInformationService.attestationFalse(updateWrapper);
            if(flag){
                returnVO.setMessage("取消认证成功！");
                returnVO.setCode("200");
            }else{
                returnVO.setMessage("取消认证失败！");
                returnVO.setCode("500");
            }
        }catch(Exception e){
            log.error(e.getMessage());
        };
        return returnVO;
    }
}
